<template>
  <div class="app-container">
    <div v-hasPermi="['sysAppManager:post:findPage']" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="name" label="应用名称">
          <el-input v-model="searchData.name" placeholder="请输入应用名称" maxlength="50" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-button v-hasPermi="['sysAppManager:post:add']" type="primary" plain :icon="Plus" @click="handleAdd">
            新增
          </el-button>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" max-height="64vh">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="name" label="应用名称" align="center" />
          <el-table-column prop="appIconUrl" label="应用图标" align="center">
            <template #default="scope">
              <span class="appIcon">
                <img v-if="scope.row.appIconUrl" :src="scope.row.appIconUrl" alt="" />
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="修改时间" align="center">
            <template #default="scope">
              {{ scope.row.updateTime ? dayjs(scope.row.updateTime).format('YYYY-MM-DD HH:mm:ss') : '' }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200" align="center">
            <template #default="scope">
              <div class="flex items-center">
                <el-button
                  v-hasPermi="['sysAppManager:put:update']"
                  type="primary"
                  text
                  bg
                  size="small"
                  @click="handleUpdate(scope.row)"
                >
                  修改
                </el-button>
                <el-button
                  v-hasPermi="['sysAppManager:delete:delete']"
                  type="danger"
                  text
                  bg
                  size="small"
                  @click="handleDelete(scope.row)"
                >
                  删除
                </el-button>
                <el-dropdown
                  v-hasPermi="['sysAppManager:get:selectSecret', 'sysAppManager:get:resetSecret']"
                  class="operation-more"
                >
                  <span class="el-dropdown-link">
                    更多
                    <el-icon class="el-icon--right">
                      <arrow-down />
                    </el-icon>
                  </span>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item @click="selectSecret(scope.row)">
                        <div v-hasPermi="['sysAppManager:get:selectSecret']" class="dropdown-item">查看密钥</div>
                      </el-dropdown-item>
                      <el-dropdown-item @click="resetSecret(scope.row)">
                        <div v-hasPermi="['sysAppManager:get:resetSecret']" class="dropdown-item">重置密钥</div>
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex justify-end">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :small="true"
        />
      </div>
    </div>
    <AddApp
      ref="addAppRef"
      v-model:visible="pageData.isShowAddDialog"
      :id="pageData.editingId"
      @addSuccess="addSuccess"
      @update="updateParameter"
    />
    <SelectSecret v-model:visible="pageData.isShowSecretDialog" :id="pageData.editingId" />
  </div>
</template>

<script setup>
import { addParameter, deleteParameter, updateParameterApi, queryParameter } from '@/api/system/parameter'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, Plus, Delete, ArrowDown } from '@element-plus/icons-vue'
import { usePagination } from '@/hooks/usePagination'
import AddApp from './components/addApp.vue'
import SelectSecret from './components/selectSecret.vue'
import dayjs from 'dayjs'
import { appManagerList, appManagerDel, resetSecretApi } from '@/api/system/app'
const loading = ref(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const addAppRef = ref(null)
const pageData = reactive({
  isShowAddDialog: false,
  isShowSecretDialog: false,
  editingId: ''
})

const addSuccess = () => {
  pageData.isShowAddDialog = false
  getTableData()
}

const handleDelete = (row) => {
  ElMessageBox.confirm(`正在删除应用：${row.name}，确认删除？`, 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    appManagerDel(row.appManagerId).then(() => {
      ElMessage.success('删除成功')
      getTableData()
    })
  })
}
const selectSecret = (row) => {
  pageData.editingId = row.appManagerId
  pageData.isShowSecretDialog = true
}
const resetSecret = (row) => {
  ElMessageBox.confirm(`正在重置密钥，确认重置`, 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    resetSecretApi(row.appManagerId).then(() => {
      ElMessage.success('重置密钥成功')
      getTableData()
    })
  })
}

const handleUpdate = (row) => {
  pageData.editingId = row.appManagerId
  pageData.isShowAddDialog = true
}

const tableData = ref([])
const searchFormRef = ref()
const searchData = reactive({
  name: ''
})
const getTableData = () => {
  loading.value = true
  appManagerList(searchData, {
    currentPage: paginationData.currentPage,
    size: paginationData.pageSize
  })
    .then((res) => {
      paginationData.total = parseInt(res.data.total)
      tableData.value = res.data.records
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
const handleAdd = () => {
  pageData.editingId = ''
  pageData.isShowAddDialog = true
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}

watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<style lang="scss" scoped>
.search-wrapper {
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;

  :deep(.el-button) {
    padding: 6px 12px;
  }
}

.table-wrapper {
  margin-bottom: 20px;
}

.operation-more {
  margin-left: 12px;
  font-size: 12px;

  .el-dropdown-link {
    cursor: pointer;
    color: var(--el-color-primary);
    display: flex;
    align-items: center;
  }
}
:deep(.el-dropdown-menu__item) {
  padding: 0;
}
.dropdown-item {
  padding: 5px 16px;
}
.appIcon{
  display: flex;
  justify-content: center;
  img{
    height: 50px;
    width: 80px;
  }
}
</style>
